package com.sporteamws;

import com.appengine.utils.AeLoadingCenter;
import com.appengine.utils.GsonUtils;
import com.appengine.utils.SavingAndLoadingCenter;
import com.gps.misc.ActivityCenter;

import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.content.BroadcastReceiver;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;


/*   registers as receivers for the message intent   */
public class C2DMMessageReceiver extends BroadcastReceiver 
{
	@Override
	public void onReceive(Context context, Intent intent)
	{
		String action = intent.getAction();
		Log.w("C2DM", "Message Receiver called");
		
		// get the message received from the intent 
		// message will be stored as "payload"
		if ("com.google.android.c2dm.intent.RECEIVE".equals(action)) 
		{
			Log.w("C2DM", "Received message");
			final String payload = intent.getStringExtra("payload");
			if(payload == null)
				return;
			
			//Log.w("C2DM", "dmControl: payload = " + payload);
			
			// Lets make something visible to show that we received the message
			createNotification(context, payload);
		}
	}

	public void createNotification(Context context, String payload) 
	{
		NotificationManager notificationManager = (NotificationManager) context.getSystemService(Context.NOTIFICATION_SERVICE);
				
		Notification notification = new Notification(com.sporteamws.R.drawable.new_msg,"new track received!", System.currentTimeMillis());
			
		// Hide the notification after its selected
		notification.flags |= Notification.FLAG_AUTO_CANCEL;

		/* send the message to the MessageReceivedActivity, to show the message
		   whenever the user presses on notification */
		
		//tmp
		Intent intent = new Intent(context, MessageReceivedActivity.class);
		intent.putExtra("payload", payload);
		PendingIntent pendingIntent = PendingIntent.getActivity(context, 0,intent, 0);
				
		String info = GetInfoFromStr(payload);
		String sender,msg;
		
		String loadedFB = SavingAndLoadingCenter.loadFB_ID(context);
		Intent introIntent;
		
		if(loadedFB == null) //in case no loaded FB found on disk
			return;
		else
		{
			introIntent = new Intent(context, Intro.class);
		}
		
		if(info.equals("track")) //in case reievied new track
		{
			sender = GetAditionalFromStr(payload);
			notification.setLatestEventInfo(context, "Track","recieved new track from " + sender, pendingIntent);
		//	context.startActivity(introIntent);
		}
		
		if(info.equals("msg")) //in case reievied new msg
		{
			msg = GetAditionalFromStr(payload);
			notification.setLatestEventInfo(context, "Message",msg, pendingIntent);
		}
		
		//notify client with new notification
		notificationManager.notify(0, notification);

	}
	
	private String GetInfoFromStr(String str)
	{
		StringBuilder msgInfo = new StringBuilder();
		for(int i = 0; i < str.length(); i++)
		{
			if(str.charAt(i) == ';') 
				break;
			else
				msgInfo.append(str.charAt(i));
		}
		
		return msgInfo.toString();
	}
	
	private String GetAditionalFromStr(String str)
	{
		StringBuilder additional = new StringBuilder();
		int counter = 0;
		for(int i = 0; i < str.length(); i++)
		{
			if(str.charAt(i) == ';'){
				if(counter == 1)
					break;
				else
				    counter++;
			}
			else if(counter == 1){
				additional.append(str.charAt(i));
			}
		}
		
		return additional.toString();
	}
	

}